package com.group8.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.font.TextAttribute;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;









import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class HighScorePanel extends Menu {
	JButton back;
	@SuppressWarnings("unchecked")
	public HighScorePanel() {
		int x = 200;
		int y = 100;
		setPreferredSize(new Dimension(800,660));
		JLabel title = new JLabel("High Scores");
		title.setFont(new Font("nfont",Font.PLAIN, 50));
		title.setForeground(Color.RED);
		setLayout(null);
		title.setBounds(400 - (int)(title.getPreferredSize().getWidth() / 2), 10,(int) title.getPreferredSize().getWidth(),(int) title.getPreferredSize().getHeight());
		back = new JButton();
		Image img = new ImageIcon("assets/main_menu/back.png").getImage();
		back.setIcon(new ImageIcon(img));
		back.setRolloverEnabled(false);
		back.setBounds(500,500,93,40);
		back.addActionListener(new ButtonListener());
		
		org.json.simple.parser.JSONParser parser = new org.json.simple.parser.JSONParser();
		
		
		
		
		try {
			Object obj = parser.parse(new FileReader("assets/highscores.json"));
			
			JSONArray jsonArray = (JSONArray) obj;
			
			Iterator<JSONObject> iterator = jsonArray.iterator();
			JLabel nameLabel = new JLabel("Name");
			Font font = new Font("nFont",Font.PLAIN, 30);
			
			Map attributes = font.getAttributes();
			attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
			nameLabel.setFont(font.deriveFont(attributes));
			
			nameLabel.setBounds(x,y, (int)nameLabel.getPreferredSize().width, (int)nameLabel.getPreferredSize().height);
			nameLabel.setForeground(Color.RED);
			add(nameLabel);
			
			
			JLabel pointsLabel = new JLabel("Points", SwingConstants.CENTER);
			pointsLabel.setFont(font.deriveFont(attributes));
			pointsLabel.setBounds(800 - x - 100, y, pointsLabel.getPreferredSize().width + 100, pointsLabel.getPreferredSize().height);
			pointsLabel.setForeground(Color.RED);
			add(pointsLabel);
			int index = 1;
			int incrementCons = (int)pointsLabel.getPreferredSize().height;
			y += incrementCons;
			int fontSize = 40;
			Font indexLabelFont = new Font("indexFont",Font.ITALIC, fontSize);
			Font otherFont = new Font("oFont",Font.PLAIN,fontSize);
			int itemCount = 0;
			while(iterator.hasNext() && itemCount < 5) {
				JSONObject jsonObj = iterator.next();
				JLabel indexLabel = new JLabel(new Integer(index).toString() + "-");
				indexLabel.setFont(indexLabelFont);
				indexLabel.setBounds(x-50, y, (int)indexLabel.getPreferredSize().width, (int)indexLabel.getPreferredSize().height);
				indexLabel.setForeground(Color.BLUE);
				index++;
				
				JLabel usernameLabel = new JLabel((String)jsonObj.get("username"));
				JLabel pointLabel = new JLabel(jsonObj.get("hscore").toString(),SwingConstants.RIGHT);
				
				usernameLabel.setFont(otherFont);
				usernameLabel.setBounds(x,y, usernameLabel.getPreferredSize().width, usernameLabel.getPreferredSize().height);
				usernameLabel.setForeground(Color.RED);
//				
				
				pointLabel.setFont(otherFont);
				pointLabel.setBounds(pointsLabel.getX() + pointsLabel.getWidth() - (int)pointLabel.getPreferredSize().width - 50, y, (int)pointLabel.getPreferredSize().width, (int)pointLabel.getPreferredSize().height);
				pointLabel.setForeground(Color.RED);
				
				
				add(indexLabel);
				add(usernameLabel);
				add(pointLabel);
				
				y += incrementCons;
				itemCount ++;
		}
			
		} catch(Exception e) {
			System.out.println("Iteration is not started");
			this.addNoOneEverPlayedTheGame();
		}
		
		
		add(title);
		add(back);
		
	}
	
	private void addNoOneEverPlayedTheGame() {
		JLabel label = new JLabel("HIGH SCORER? DID YOU PLAYED?",SwingConstants.CENTER);
		
		label.setFont(new Font("fnt",Font.BOLD, 40));
		label.setBounds(0, 100 , 800, label.getPreferredSize().height);
		label.setForeground(Color.RED);
		
		add(label);
		
	}
	
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Image img = new ImageIcon("assets/trophy.png").getImage();
		g.drawImage(img, 0, 0, null);
	}
	
private class ButtonListener extends MenuActionListener{
		
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == back){
				switchPanels(getMain());
				System.out.println("Back clicked");
			}
			
			
		}
		
	}
}
